/****************************************************************************
 * 2025.7 GRAVEROBBER-PC
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using Unity.VisualScripting;
using UnityEngine.Rendering.Universal;
using UnityEngine.U2D;

namespace ProjectSurvival
{
	public partial class UnlockedIconPanel : UIElement,IController
	{
		private Dictionary<string, QFramework.Tuple<LevelUpItem, Image>> _unlockedKeys = new(); 

		private ResLoader _resLoader = ResLoader.Allocate();
		private LevelUpSystem _lvSystem;
		private SpriteAtlas _iconAtlas;
		private void Awake()
		{
			
			UnlockedIconTemp.Hide();
			_resLoader.Add2Load<SpriteAtlas>("Icon");
			_resLoader.LoadAsync(() =>
			{
				_iconAtlas= _resLoader.LoadSync<SpriteAtlas>("Icon");
			});
			//var iconAtlas = _resLoader.LoadSync<SpriteAtlas>("Icon");
			_lvSystem = this.GetSystem<LevelUpSystem>();
			
			foreach (var levelUpItem in _lvSystem.LevelUpItems)
			{
				var itemCatch = levelUpItem;
				levelUpItem.currentLevel.RegisterWithInitValue(level =>
				{
					if (level>0)
					{
						if (_unlockedKeys.ContainsKey(itemCatch.Key))
						{
							
						}
						else
						{
							UnlockedIconTemp.InstantiateWithParent(UnlockedRoot)
								.Self(self =>
								{
									self.sprite = _iconAtlas.GetSprite(itemCatch.IconName);
									_unlockedKeys.Add(itemCatch.Key,new QFramework.Tuple<LevelUpItem, Image>(itemCatch,self));
								})
								.Show();
						}
					}
					
				}).UnRegisterWhenGameObjectDestroyed(gameObject);
			}

			Debug.Log("keysCount:"+_unlockedKeys.Count);
			foreach (var key in _unlockedKeys)
			{
				Debug.Log($"key:{key.Key},itemName:{key.Value.Item1.Name},itemKey:{key.Value.Item1.Key},itemImage:{key.Value.Item2.sprite.name}");
			}

			Global.SuperSword.Register(isUnlocked =>
			{
				if (isUnlocked)
				{
					var currentItemKey = "simple_Sword";
					if (_unlockedKeys.ContainsKey(currentItemKey))
					{
						var item = _unlockedKeys[currentItemKey].Item1;
						var sprite = _iconAtlas.GetSprite(item.PairedIconName);
						_unlockedKeys[currentItemKey].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperKnife.Register(isUnlocked =>
			{
				if (isUnlocked)
				{
					var currentItemKey = "simple_knife";
					if (_unlockedKeys.ContainsKey(currentItemKey))
					{
						var item = _unlockedKeys[currentItemKey].Item1;
						var sprite = _iconAtlas.GetSprite(item.PairedIconName);
						_unlockedKeys[currentItemKey].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperRotateSword.Register(isUnlocked =>
			{
				if (isUnlocked)
				{
					var currentItemKey = "simple_Rotate_Sword";
					if (_unlockedKeys.ContainsKey(currentItemKey))
					{
						var item = _unlockedKeys[currentItemKey].Item1;
						var sprite = _iconAtlas.GetSprite(item.PairedIconName);
						_unlockedKeys[currentItemKey].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperBasketBall.Register(isUnlocked =>
			{
				if (isUnlocked)
				{
					var currentItemKey = "simple_BasketBall";
					if (_unlockedKeys.ContainsKey(currentItemKey))
					{
						var item = _unlockedKeys[currentItemKey].Item1;
						var sprite = _iconAtlas.GetSprite(item.PairedIconName);
						_unlockedKeys[currentItemKey].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperBomb.Register(isUnlocked =>
			{
				if (isUnlocked)
				{
					var currentItemKey = "simple_Bomb";
					if (_unlockedKeys.ContainsKey(currentItemKey))
					{
						var item = _unlockedKeys[currentItemKey].Item1;
						var sprite = _iconAtlas.GetSprite(item.PairedIconName);
						_unlockedKeys[currentItemKey].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			_resLoader.Recycle2Cache();
			_resLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}